package com.example.usermanagement.liuka.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.usermanagement.exception.ObjectNotException;
import com.example.usermanagement.exception.ParamErrorException;
import com.example.usermanagement.liuka.model.FlowUser;
import com.example.usermanagement.liuka.service.FlowLogService;
import com.example.usermanagement.liuka.service.FlowUserService;
import com.example.usermanagement.vo.FlowUserShopVO;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.Objects;

/**
  *购买用户管理
 */
@AllArgsConstructor
@RestController
@RequestMapping("/flow-user")
@Validated
public class FlowUserController {

    private final FlowUserService flowUserService;
    private final FlowLogService flowLogService;


    /**
     * 添加使用用户
     * @param name 姓名
     * @param phone 电话号码
     * @param cardNo 身份证号
    **/
    @PostMapping()
    public void addFlowUser(@NotBlank(message = "姓名不能为空")String name,
                            @NotBlank(message = "电话号码不能为空")String phone,
                            @NotBlank(message = "身份证号不能为空")String cardNo) throws ParamErrorException {
        FlowUser flowUser = flowUserService.getOne(Wrappers.lambdaQuery(new FlowUser())
                .eq(FlowUser::getPhone,phone));
        if(Objects.nonNull(flowUser)){
            throw new ParamErrorException("电话号码已经存在");
        }
        flowUser = FlowUser.builder().name(name).phone(phone).cardNo(cardNo).build();
        flowUserService.save(flowUser);
    }


    /**
     *  使用用户列表分页
     * @param page 第几页 默认1
     * @param pageSize 每页条数 默认20
     * @param phone  电话号码
     * @param name  姓名
     * @param cardNo  身份证号
     * @return
    **/
    @GetMapping("/page")
    public IPage<FlowUser> getPage(@RequestParam(defaultValue = "1")Long page,
                                   @RequestParam(defaultValue = "20")Long pageSize,
                                   String phone,String name,String cardNo){
        LambdaQueryWrapper<FlowUser> wrapper = Wrappers.lambdaQuery(new FlowUser());
        if(StrUtil.isNotBlank(phone)){
            wrapper.like(FlowUser::getPhone,phone);
        }
        if(StrUtil.isNotBlank(name)){
            wrapper.like(FlowUser::getName,name);
        }
        if(StrUtil.isNotBlank(cardNo)){
            wrapper.like(FlowUser::getCardNo,cardNo);
        }
        wrapper.orderByDesc(FlowUser::getId);
        return flowUserService.page(new Page<>(page,pageSize),wrapper);
    }

    /**
     * 购买用户详情
     * @param id id
     * @return com.example.usermanagement.vo.FlowUserShopVO
     **/
    @GetMapping("/{id}")
    public FlowUserShopVO getDetail(@PathVariable Long id) throws ObjectNotException {
        return flowLogService.getDetail(id);
    }

}
